package com.skywolf.chem.article.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.skywolf.chem.article.entity.Article;
import com.skywolf.chem.article.entity.CollectionInfo;
import com.skywolf.chem.article.entity.Position;
import com.skywolf.chem.article.pojo.ArticleQueryParam;
import com.skywolf.chem.article.service.IArticleService;
import com.skywolf.chem.article.service.ICollectionInfoService;
import com.skywolf.chem.common.sysconfig.pojo.vo.PageVO;
import com.skywolf.chem.common.sysconfig.response.RespData;
import com.skywolf.chem.usercenterapi.service.IPositionService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 帖子 前端控制器
 * </p>
 *
 * @author 
 * @since 2020-06-09
 */
@RestController
@RequestMapping("/ct/article/article")
public class ArticleController {

    @Autowired
    IArticleService articleService;

    @Autowired
    ICollectionInfoService collectionInfoService;

    @Autowired
    IPositionService positionService;

    @ApiOperation(value = "根据栏目，查询栏目下帖子",notes = "根据栏目，查询栏目下帖子")
    @RequestMapping(value = "/getArticleByCol",method = {RequestMethod.POST},produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespData<Article> getArticleByCol(@RequestBody @Validated ArticleQueryParam queryParam) {
        queryParam.setArtStatus(3);
        PageVO<Article> page = articleService.pageArticleList(queryParam);
        return RespData.success(page);
    }

    @ApiOperation(value = "根据帖子id，查询帖子详情",notes = "根据帖子id，查询帖子详情")
    @RequestMapping(value = "/getArticleDetail",method = {RequestMethod.GET,RequestMethod.POST},produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespData<Article> getArticleDetail(HttpServletRequest request,@RequestParam(value = "id",required = true) Long id) {
        Article article = articleService.getById(id);
        if(article!=null){
//            String imgs =  article.getCoverUrl();
//            if(StringUtils.isNotBlank(imgs)){
//                String[] list = imgs.split(",");
//                if(list.length>0){
//                    String img = list[0];
//                    img = img+"?x-oss-process=image/resize,h_100,m_lfit";
//                    article.setShareImg(img);
//                }
//            }
            if(StringUtils.isBlank(article.getShareImg())){
                article.setShareImg("https://chuanti-bucket.oss-cn-chengdu.aliyuncs.com/img/a1942a38-8d30-4eff-8715-10322cd1854c.png?x-oss-process=image/resize,l_100,m_mfit");
            }
            LocalDateTime time = article.getAddTime();
            if(time==null){
                time = article.getCreatedTime();
            }
            article.setViewCount(article.getViewCount()+1);
//            article.setCreatedTime(null);
//            article.setAddTime(null);
//            article.setUpdatedTime(null);
            articleService.updateById(article);
            if(time!=null){
                article.setCreatedTime(time);
                article.setAddTime(time);
            }
            String uid = request.getHeader("user_id");
            if(StringUtils.isNotBlank(uid)){
                QueryWrapper<CollectionInfo> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("cus_uid", uid);
                queryWrapper.eq("coll_id", id);
                CollectionInfo collectionInfo = collectionInfoService.getOne(queryWrapper);
                if(collectionInfo!=null){
                    article.setCollection(1);
                }else{
                    article.setCollection(0);
                }
            }else{
                article.setCollection(0);
            }
            Integer viewCont = article.getViewCount()+article.getBaseViewCount().intValue();
            article.setViewCount(viewCont);

            Integer collCont = article.getCollectionCount()+article.getBaseCollectionCount();
            article.setCollectionCount(collCont);
            //查询帖子所在栏目
            QueryWrapper<Position> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(Position::getArticleId, article.getId());
            Position position = positionService.getOne(queryWrapper);
            List<Article> articles = new ArrayList<>();
            if(position!=null){
                articles = positionService.getArticleByColumnId(position.getColumnId(),id);
            }
            if(articles==null){
                articles = positionService.getRecommendnews(id);
            }
            article.setRecommendNews(articles);
        }
        return RespData.success(article);
    }

}
